//
//  AdventureController.m
//  Food
//
//  Created by dllo on 15/7/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AdventureController.h"
#import "Adventure_TableViewCell.h"
#import "AFNetworkHandler.h"
#import "BaseHeader.h"
#import "MJRefresh.h"
#import "AdventureDetailVController.h"
static int i = 1;
@interface AdventureController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation AdventureController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.barTintColor= [UIColor colorWithRed:238 / 255.0 green:236 / 255.0 blue:232 / 255.0 alpha:1];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.navigationItem.title = @"新鲜事";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.95 green:0.94 blue:0.93 alpha:1];
    
    [self createTableView];
    
    [self getData];
    
    [self setupRefresh];
}

- (void) viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.hidden = NO;
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 113) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
    self.tableView.showsVerticalScrollIndicator = NO;
    //UIScrollView
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [_tableView release];
}

- (void)getData
{
    UIActivityIndicatorView *activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
    
    activityIndicator.frame = CGRectMake(0, 0, 100, 100);
    
    activityIndicator.color = [UIColor grayColor];
    
    activityIndicator.center = CGPointMake(ScreenWidth / 2.0, ScreenHeight / 2.0 - 50);
    
    [self.view addSubview:activityIndicator];
    
    [activityIndicator startAnimating];
    NSString *str = @"http://121.41.84.91:80/HandheldKitchen/api/found/tblFresh!getTblFreshList.do?is_traditional=0&page=1&pageRecord=10&phonetype=1";
    [AFNetworkHandler getWithAFNByURL:str completion:^(id result) {
        NSArray *tempArr = [result objectForKey:@"data"];
        self.adventureArray = [AdventureModel arrayWithModelByArray:tempArr];
        [self.tableView reloadData];
        [activityIndicator stopAnimating];
    }];
}

- (void) setupRefresh
{
    [self.tableView addFooterWithTarget:self action:@selector(headerRereshing)];
}

- (void)headerRereshing
{
    i++;
    NSString *strURl = [NSString stringWithFormat:@"http://121.41.84.91:80/HandheldKitchen/api/found/tblFresh!getTblFreshList.do?is_traditional=0&page=%d&pageRecord=10&phonetype=1", i];
    [AFNetworkHandler getWithAFNByURL:strURl completion:^(id result) {
        NSArray *tempArr = [result objectForKey:@"data"];
        NSArray *tempArry = [AdventureModel arrayWithModelByArray:tempArr];
        if (tempArr.count == 0) {
            self.tableView.footerRefreshingText = @"数据加载完毕";
        }
            for (AdventureModel *model in tempArry) {
            [self.adventureArray addObject:model];
        }
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
        [self.tableView footerEndRefreshing];
    });
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Adventure_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"adventure"];
    if (!cell) {
        cell = [[Adventure_TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"adventure"];
    }
    AdventureModel *adventure = self.adventureArray[indexPath.row];
    cell.adventure = adventure;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.adventureArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120.0 / 375 * ScreenWidth;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AdventureModel *model = self.adventureArray[indexPath.row];
    AdventureDetailVController *adventureDetail = [[AdventureDetailVController alloc] init];
    adventureDetail.freshid = model.freshId;
    adventureDetail.string = model.name;
    adventureDetail.content = model.content;
    adventureDetail.titleImageFile = model.titleImageFile;
    [self.navigationController pushViewController:adventureDetail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
